<?php
/**
 * 图纸模型
 * Class Drawing
 * @package app\index\model
 */

namespace app\index\model;


class Drawing extends Base
{
    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:图纸列表
     * @param string $keyword
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function drawingList($keyword = "",$style = "",$layct = "",$structure="",$house="",$is_top="",$orders="",$after_limit="10"){
        if($orders){
            $orderName = $orders["type"];
            $orderType = $orders["type_order"];
        }else{
            $orderName = "drawing_id";
            $orderType = "desc";
        }
        return $this->alias('a')->field('a.*,b.config_name')->join('shop_config b','a.style = b.config_id','left')
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    //模糊查找支持标题和编号
                    $q->whereOr("no",'like','%'.$keyword.'%')->whereOr("topic",'like','%'.$keyword.'%');
                }
            })
            ->where(function ($q)use ($style){
                if(!empty($style)){
                    //分类查找
                    $q->where("style",$style);
                }
            })
            ->where(function ($q)use ($layct){
                if(!empty($layct)){
                    //楼层查找
                    if($layct==5){
                        $q->where("layct >=5",$layct);
                    }else{
                        $q->where("layct",$layct);
                    }
                }
            })
            ->where(function ($q)use ($is_top){
                if(!empty($is_top)){
                    //是否置顶
                    $q->where("is_top",$is_top);
                }
            })
            ->where(function ($q)use ($structure){
                if(!empty($structure)){
                    //分类查找
                    $q->where("struct",$structure);
                }
            })
            ->where(function ($q)use ($house){
                if(!empty($house)){
                    //户型查找
                    $q->where("attrs","like","%[".$house."]%");
                }
            })
            ->where("is_shel",1)
            ->orderRaw("$orderName $orderType")
            ->paginate($after_limit,false,[
                'type'     => 'page\marketPage',
                'var_page'  => 'p',
                'query' =>  request()->param(),
            ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-09 10:27:51
     * Describe:图纸列表总数
     * @param string $keyword
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function drawingListCount($keyword = "",$style = "",$layct = "",$structure="",$house="",$is_top=""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    //模糊查找支持标题和编号
                    $q->whereOr("no",'like','%'.$keyword.'%')->whereOr("topic",'like','%'.$keyword.'%');
                }
            })
            ->where(function ($q)use ($style){
                if(!empty($style)){
                    //分类查找
                    $q->where("style",$style);
                }
            })
            ->where(function ($q)use ($layct){
                if(!empty($layct)){
                    //楼层查找
                    if($layct==5){
                        $q->where("layct >=5",$layct);
                    }else{
                        $q->where("layct",$layct);
                    }
                }
            })
            ->where(function ($q)use ($structure){
                if(!empty($structure)){
                    //分类查找
                    $q->where("struct",$structure);
                }
            })
            ->where(function ($q)use ($is_top){
                if(!empty($is_top)){
                    //是否置顶
                    $q->where("is_top",$is_top);
                }
            })
            ->where("is_shel",1)
            ->where(function ($q)use ($house){
                if(!empty($house)){
                    //户型查找
                    $q->where("attrs","like","%[".$house."]%");
                }
            })
            ->count();
    }
    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:感兴趣的图纸
     * @param string $keyword
     * @param string $cate
     * @param string $layct
     * @return int|string
     */
    public function  interestDrawing($limit="4"){
        return $this->alias('a')->field('a.drawing_id,a.topic,a.layct,a.cover,c.config_name')->join('shop_config c','a.style = c.config_id','left')
            ->where("a.is_shel",1)
            ->where("a.delete",0)
            ->orderRaw("a.reading_number desc")
            ->limit($limit)
            ->select();
    }

}